package com.jxl.dnd.charactercreator.vo.abilityscores
{
	import flash.events.EventDispatcher;
	

	[Bindable]
	public class AbilityScores extends EventDispatcher
	{
		public var strength:AbilityScore;
		[Bindable(event="dexterityChanged")]
		public var dexterity:AbilityScore;
		public var constitution:AbilityScore;
		public var intelligence:AbilityScore;
		public var wisdom:AbilityScore;
		public var charisma:AbilityScore;
		
		public function AbilityScores(strength:uint=10,
										dexterity:uint=10,
										constitution:uint=10,
										intelligence:uint=10,
										wisdom:uint=10,
										charisma:uint=10):void
		{
			super();
			
			this.strength			= new AbilityScore(AbilityScore.STRENGTH, strength);
			this.dexterity			= new AbilityScore(AbilityScore.DEXTERITY, dexterity);
			this.constitution		= new AbilityScore(AbilityScore.CONSTITUTION, constitution);
			this.intelligence		= new AbilityScore(AbilityScore.INTELLIGENCE, intelligence);
			this.wisdom				= new AbilityScore(AbilityScore.WISDOM, wisdom);
			this.charisma			= new AbilityScore(AbilityScore.CHARISMA, charisma);
			
			this.dexterity.addEventListener("valueChanged", onAbilityScoreChanged);
		}
		
		protected function onAbilityScoreChanged(event:Event):void
		{
			//DebugWindow.header();
			//DebugWindow.debug("AbilityScores::onAbilityScoreChanged, bubbling up");
			dispatchEvent(new Event("dexterityChanged", true));
		}
		
		public function buildFromObject(obj:Object):void
		{
			this.strength.buildFromObject(obj);
			this.dexterity.buildFromObject(obj);
			this.constitution.buildFromObject(obj);
			this.intelligence.buildFromObject(obj);
			this.wisdom.buildFromObject(obj);
			this.charisma.buildFromObject(obj);
		}
		
		public function toObject():Object
		{
			var obj:Object 		= new Object();
			obj.strength		= strength.toObject();
			obj.dexterity		= dexterity.toObject();
			obj.constitution	= constitution.toObject();
			obj.intelligence	= intelligence.toObject();
			obj.wisdom			= wisdom.toObject();
			obj.charisma		= charisma.toObject();
			return obj;
		}
		
		public function toXML():XML
		{
			var myXML:XML = 
				<abilityscores>
				</abilityscores>;
				
			if(strength != null) myXML.appendChild(strength.toXML());
			if(dexterity != null) myXML.appendChild(dexterity.toXML());
			if(constitution != null) myXML.appendChild(constitution.toXML());
			if(intelligence != null) myXML.appendChild(intelligence.toXML());
			if(wisdom != null) myXML.appendChild(wisdom.toXML());
			if(charisma != null) myXML.appendChild(charisma.toXML());
			return myXML;
		}
		
		public function buildFromXML(xml:XML):void
		{
			strength.buildFromXML(xml, AbilityScore.STRENGTH);
			dexterity.buildFromXML(xml, AbilityScore.DEXTERITY);
			constitution.buildFromXML(xml, AbilityScore.CONSTITUTION);
			intelligence.buildFromXML(xml, AbilityScore.INTELLIGENCE);
			wisdom.buildFromXML(xml, AbilityScore.WISDOM);
			charisma.buildFromXML(xml, AbilityScore.CHARISMA);
		}
	}
}